package com.itboye.lottery.activity.user;

import android.support.v4.content.ContextCompat;
import android.view.View;

import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import com.itboye.lottery.base.BaseActivity;
import com.itboye.lottery.R;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.annotions.IsNeedClick;
import com.itboye.lottery.bean.AddressBean;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.citywheel.CityPickerView;
import org.jetbrains.annotations.NotNull;

public class AddressEditActivity extends BaseActivity implements ResponseCallback {


    AddressBean.AddressItem addressBean;
    @IsNeedClick
    EditText consignee_name, consignee_phone, detail;
    TextView place;
    CheckBox cb_address_detail;
    TextView save_btn;
    UserPresenter userPresenter;
    private CityPickerView cityPicker;
    private String province;
    private String city;
    private String district;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address_edit;
    }

    @Override
    protected void initData() {
        userPresenter = new UserPresenter(this);
        addressBean = (AddressBean.AddressItem) getIntent().getSerializableExtra("model");
        if (addressBean != null) {
            //修改地址
            setTitle("修改地址");
            setAddressDetail();
        } else {
            setTitle("添加地址");
        }
    }

    private void setAddressDetail() {
        consignee_name.setText(addressBean.getConsignee());
        consignee_phone.setText(addressBean.getMobile());
        detail.setText(addressBean.getAddress());
        this.province = addressBean.getProvince();
        this.city = addressBean.getCity();
        this.district = addressBean.getDistrict();
        place.setText(addressBean.getProvince() + addressBean.getCity() + addressBean.getDistrict());
        if (addressBean.getIs_default() == 1) {
            cb_address_detail.setChecked(true);
        } else {
            cb_address_detail.setChecked(false);
        }
    }

    @Override
    public void onClick(View view) {
        if (view == save_btn) {
            //保存修改的地址
            if (!isInfoEmpty()) {
                String consigneeName = consignee_name.getText().toString();
                String consigneeMobile = consignee_phone.getText().toString();
                String pcd = place.getText().toString();
                String addressDetail = detail.getText().toString();
                String isDefault = cb_address_detail.isChecked() ? "1" : "0";
                if (addressBean != null) {
                    userPresenter.updateAddress(addressBean.getAddress_id(),
                            consigneeName,
                            consigneeMobile, province, city, district, pcd, isDefault);
                } else {
                    //新增一个地址
                    userPresenter.addAddress(consigneeName,
                            consigneeMobile, province, city, district, pcd, isDefault);
                }
            } else {

            }
        } else if (view == place) {
            showCityPicker();
        }
    }

    private boolean isInfoEmpty() {
        if (consignee_name.getText().toString().isEmpty()) {
            ByAlert.alert("请填写收货人姓名");
            return true;
        }
        if (consignee_phone.getText().toString().isEmpty()) {
            ByAlert.alert("请填写手机号码");
            return true;
        }
        if (detail.getText().toString().isEmpty()) {
            ByAlert.alert("请选择所在地区");
            return true;
        }
        if (place.getText().toString().isEmpty()) {
            ByAlert.alert("请填写详细地址");
            return true;
        }
        return false;
    }

    private void showCityPicker() {
        CityConfig cityConfig = new CityConfig.Builder(this)
                .textSize(18)//（滚轮文字的大小，int 类型，默认为18）
                .title("地区选择")//（选择器标题，默认为“选择地区”）
                .titleBackgroundColor("#ffffff")//（标题栏背景，默认为灰色，#C7C7C7）
                .titleTextColor("#333333")//(标题文字颜色，默认为 #E9E9E9)
                .confirTextColor("#666666")//（确认按钮字体颜色，默认为系统的colorPrimary颜色值）
                .cancelTextColor("#666666")//（取消按钮字体颜色，默认为系统的colorPrimary颜色值）
                .province("浙江")//（默认的显示省份，显示选择器后直接定位的item位置）
                .city("杭州")//（默认的显示市，显示选择器后直接定位的item位置）
                .district("江干区")//（默认的显示区，显示选择器后直接定位的item位置）
                .textColor("#333333")//（滚轮文字的颜色 ，int 类型，默认为0xFF585858）
                .provinceCyclic(true)//（省份的滚轮是否循环滚动）
                .cityCyclic(false)//（市的滚轮是否循环滚动）
                .districtCyclic(false)//（区的滚轮是否循环滚动）
                .visibleItemsCount(7)//（滚轮显示的item个数，int 类型，默认为5个）
                .itemPadding(10)//（滚轮item间距，默认为5dp）
                .setCityInfoType(CityConfig.CityInfoType.BASE) //BASE:只包含省市区名称; DETAIL:包含详细的经纬度、code、省市区数据名称等数据
                .setCityWheelType(CityConfig.WheelType.PRO_CITY_DIS)
                .build();
        cityPicker = new CityPickerView(cityConfig);
        //监听方法，获取地址选择结果
        cityPicker.setOnCityItemClickListener(new CityPickerView.OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                AddressEditActivity.this.province = province.getName();
                AddressEditActivity.this.city = city.getName();
                AddressEditActivity.this.district = district.getName();
                place.setText(province.getName() + city.getName() + "市" + (district == null ? "" : district.getName()));
                place.setTextColor(ContextCompat.getColor(AddressEditActivity.this, R.color.text_black));
            }

            @Override
            public void onCancel() {
            }
        });
        cityPicker.show();
    }


    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
//        if (requestSuccessType == userPresenter.updateAddress_SUCCESS) {
//
//
//
//        } else if (requestSuccessType == userPresenter.addAddress_SUCCESS) {
//
//        }
        ByAlert.alert(o);
        finish();
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        if (requestErrorType == userPresenter.updateAddress_FAIL) {
            ByAlert.alert(msg);
        } else if (requestErrorType == userPresenter.addAddress_FAIL) {
            ByAlert.alert(msg);
        }
    }
}
